package com.car.service;

import com.car.dao.CarDao;
import com.car.dao.PurchaseRecordDao;
import com.car.dao.UserCarDao;
import com.car.dao.UserDao;
import com.car.dao.impl.CarDaoImpl;
import com.car.dao.impl.PurchaseRecordImpl;
import com.car.dao.impl.UserCarDaoImpl;
import com.car.domain.Car;
import com.car.domain.PurchaseRecord;
import com.car.domain.User;
import com.car.domain.UserCar;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Scanner;

/**
 * 用户业务服务类
 */
public class UserService {
    private final UserDao userDao;
    private final static UserCarDao userCarDao = new UserCarDaoImpl();
    private final static CarDao carDao = new CarDaoImpl();
    private final static PurchaseRecordDao purchaseRecordDao = new PurchaseRecordImpl();

    public UserService(UserDao USER_DAO) {
        this.userDao = USER_DAO;
    }

    /**
     * 用户注册
     */
    public boolean register() {
        User user = new User();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String userName = scanner.next();
        user.setName(userName);
        System.out.println("请输入电话号码：");
        String phone = scanner.next();
        user.setPhone(phone);
        System.out.println("请输入密码：");
        String password = scanner.next();
        user.setPassword(password);
        System.out.println("请输入邮箱：");
        String email = scanner.next();
        user.setEmail(email);
        return userDao.register(user);

    }

    /**
     * 用户登录
     * 输入手机号和密码
     *
     * @return 登录的用户
     */
    public User login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入电话号码：");
        String phone = scanner.next();
        System.out.println("请输入密码：");
        String password = scanner.next();
        return this.userDao.login(phone, password);

    }

    public List<Car> ownCars(User user) {
        //String sql="select * from t_user_car tuc left join t_car tc on tuc.car_id=tc.id where tuc.user_id=?";
        return userDao.ownCars(user.getId());
    }


    /**
     * 购买汽车
     *
     * @param user    用户
     * @param scanner scanner
     */
    public void buyCar(User user, Scanner scanner) throws Exception {
        System.out.println("购买汽车");
        //让用户输入购买汽车的id
        System.out.println("请输入购买汽车id：");
        int carId=scanner.nextInt();
        //在数据库查询 汽车是否存在 并且状态未销售  status 0未销售 1已销售
        Car car= carDao.findByIdAndStatusEq0(carId);
        //如果在数据库中没有查询到 购买失败
        if (car==null){
            System.out.println("购买失败");
            return;
        }
        //汽车状态更新 0->1
        car.setStatus(1);
        carDao.update(car);
        //汽车和用户的中间表 新增一条记录
        UserCar userCar=new UserCar();
        userCar.setCarId(carId);
        userCar.setUserId(user.getId());
        userCarDao.add(userCar);
        //销售记录表 新增一条
        PurchaseRecord pr=new PurchaseRecord();
        pr.setUserId(user.getId());
        pr.setCarId(carId);
        pr.setEmployeeId(car.getEmployeeId());
        pr.setPurchaseDate(LocalDateTime.now());
        purchaseRecordDao.add(pr);

    }

    public void updatePassword(User user, Scanner scanner) throws Exception {
        System.out.println("修改密码");
        System.out.println("请输入新密码：");
        String password = scanner.next();
        user.setPassword(password);
        int i = userDao.update(user);
        if (i>0){
            System.out.println("更新成功");
        }else {
            System.out.println("操作失败");
        }
    }

    public void upadateUserName(User user, Scanner scanner) throws Exception {
        System.out.println("修改用户名");
        System.out.println("请输入新用户名：");
        String username = scanner.next();
        user.setPassword(username);
        int i = userDao.update(user);
        if (i>0){
            System.out.println("更新成功");
        }else {
            System.out.println("操作失败");
        }
    }
}
